package com.ahmedmisbah.andgenplus.codegenerator;

import java.io.IOException;

import org.dom4j.DocumentException;

import com.ahmedmisbah.andgenplus.codegenerator.beans.GeneratorDescriptor;
import com.ahmedmisbah.andgenplus.codegenerator.exceptions.CodeGeneratorException;
import com.ahmedmisbah.andgenplus.codegenerator.parser.CodeGeneratorXMLParser;

public class CodeGeneratorFactory {

	public static CodeGenerator buildCodeGenerator(String xmlPath)
			throws CodeGeneratorException {

		CodeGenerator ret = null;

		CodeGeneratorXMLParser parser = new CodeGeneratorXMLParser();

		try {
			GeneratorDescriptor generatorDescriptor = parser
					.parseCodeGeneratorXML(xmlPath);
			String className = generatorDescriptor.getClassPath();

			ret = (CodeGenerator) Class.forName(className).newInstance();

			ret.setDescriptor(generatorDescriptor);

		} catch (IOException e) {
			throw new CodeGeneratorException(e);
		} catch (DocumentException e) {
			throw new CodeGeneratorException(e);
		} catch (InstantiationException e) {
			throw new CodeGeneratorException(e);
		} catch (IllegalAccessException e) {
			throw new CodeGeneratorException(e);
		} catch (ClassNotFoundException e) {
			throw new CodeGeneratorException(e);
		}

		return ret;

	}

}
